package com.web.test.servlet;

import com.web.test.dao.GradeDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Created by St01 on 2017-9-25.
 */
public class MyGradeServlet extends HttpServlet {

    GradeDao gradeDao = new GradeDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String id = (String) req.getSession().getAttribute("id");

        if (req.getSession().getAttribute("isLogin") != null && (Boolean) req.getSession().getAttribute("isLogin")) {

            try {
                req.getSession().setAttribute("max", gradeDao.getMaxGrade(id));
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            req.getRequestDispatcher("webexam_my_grade.jsp").forward(req, resp);
        } else {
            req.getSession().setAttribute("isLogin", false);
            req.getSession().setAttribute("errorMsg", "请登录后访问页面");
            resp.sendRedirect("webexam_login.jsp");
        }
    }
}
